package com.lanou.tk.tourism.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.adapter.RecommendTotalAdapter;
import com.lanou.tk.tourism.entity.Total;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class RecommendTotalBtn extends Activity implements View.OnClickListener {
    List<Total> totals = new ArrayList<Total>();

    private Button ic_back2;

    private RecyclerView recyclerView;
    private RecommendTotalAdapter recommendTotalAdapter;
    private RequestQueue queue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.total_btn_recommend);

        ic_back2 = (Button) findViewById(R.id.ic_back2);
        ic_back2.setOnClickListener(this);


        recommendTotalAdapter = new RecommendTotalAdapter(this);
        recyclerView = (RecyclerView) findViewById(R.id.total_Recommend_Rv);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(recommendTotalAdapter);

        recommendTotalAdapter.setOnItemClickLitener(new RecommendTotalAdapter.OnItemClickLitener() {
                                                        @Override
                                                        public void onItemClick(View view, int position) {

                                                            Intent intent = new Intent(RecommendTotalBtn.this, RecommendTotalWeb.class);
                                                            intent.putExtra("url", totals.get(position).getUrl());
                                                            intent.putExtra("title", totals.get(position).getTitle());

                                                            startActivity(intent);
                                                            finish();

                                                        }

                                                        @Override
                                                        public void onItemLongClick(View view, int position) {

                                                        }

                                                    }
        );


        initData();

    }

    private void initData() {
        queue = Volley.newRequestQueue(this);


        String url = "http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=1445235475313&lat=38.883721&lon=121.544776&page=1&count=10";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,

                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("data");


                            Gson gson = new Gson();


                            for (int i = 0; i < jsonArray.length(); i++) {
                                String data = jsonArray.getString(i);
                                Total total = gson.fromJson(data, Total.class);
                                totals.add(total);

                            }
                            recommendTotalAdapter.addData(totals);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ic_back2:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;

        }

    }

}


